package com.chhlz.module.base.action;

import java.util.List;

import org.apache.catalina.tribes.util.Arrays;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.chhlz.core.action.BaseAction;
import com.chhlz.core.action.Json;
import com.chhlz.core.util.ExceptionUtil;
import com.chhlz.module.base.model.DishCategory;
import com.chhlz.module.service.DishCategoryService;
import com.opensymphony.xwork2.ModelDriven;


/**
 * 菜式控制器
 * @author ALEXGORDON
 *
 */
@Controller
@Action(value = "dishCategory", results = { @Result(name = "dishCategory", location = "/category.html") })
public class DishCategoryAction extends BaseAction implements ModelDriven<DishCategory> {
	private static final long serialVersionUID = 3088843221530537631L;
	private static final Logger logger = Logger.getLogger(DishCategoryAction.class);
	@Autowired
	private DishCategoryService dishCategoryService;
	public DishCategory dishCategory = new DishCategory();
	/**
	 * 得到数据列表
	 */
	public void grid(){
		int total = dishCategoryService.getTotal();
		List<DishCategory> list = dishCategoryService.findAll(page, rows, sort, order);		
//		System.out.println("dish category action grid: " + Arrays.toString(list.toArray()));
		writeJson(total,list);
	}
	/**
	 * 添加操作
	 */
	public void add(){
			Json result = new Json();
			try {
				dishCategoryService.save(dishCategory);
				result.setSuccess(true);
				result.setMsg("添加菜式类型成功");					
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加菜式类型失败");					
			}
			writeJson(result);
	}
	/**
	 * 修改操作
	 */
	public void edit() {
			Json result = new Json();
			try {
				dishCategoryService.update(dishCategory);
				result.setSuccess(true);
				result.setMsg("修改菜式类型成功");
				result.setSuccess(true);
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加菜式类型失败");
			}
			writeJson(result);
	}
	/**
	 * 删除操作
	 */
	public void remove() {
			Json result = new Json();
			try {
				dishCategoryService.delete(dishCategory.getDcId());
				result.setSuccess(true);
				result.setMsg("删除菜式类型成功");
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("删除菜式类型失败");
			}
			writeJson(result);
	}
	@Override
	public DishCategory getModel() {
		return dishCategory;
	}	
}
